package edu.groupservice.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import edu.groupservice.dao.GroupRepository;
import edu.groupservice.entity.Group;
import edu.groupservice.entity.User;
import edu.groupservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JPhoebe on 2017-7-8.
 */

@RestController
public class GroupController {

    @Value("${server.port}")
    private Integer requestPort;

    @Autowired
    private GroupRepository groupRepository;

    @Autowired
    private UserService userService;


    // 将请求方法 和 failback 方法分开，使用多线程 （推荐配置，默认配置）
//    @HystrixCommand(fallbackMethod = "findUserByGroupIdFallBack")
    // 将请求方法 和 failback 方法放在同一个线程中
    // 当bean的scope为session和request时，报错找不到上下文，就可以使用该方法
//    @HystrixCommand(
//            fallbackMethod = "findUserByGroupIdFallBack",
//            commandProperties = {
//                @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")
//            }
//    )
    @GetMapping("/group/{id}")
    public Map<String,List> findUserByGroupId(@PathVariable(name = "id") long id){

        List<Group> list = groupRepository.findAll();
        List<User> userList = new ArrayList<>();
        List<Group> groupList = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        for(Group group : list){
            if(group.getId() == id){
                temp.add(group.getUser_id());
                group.setRequestPort(requestPort);
                groupList.add(group);
            }
        }
        for(Integer idTemp:temp){
            User user = userService.findUserById(idTemp);
            userList.add(user);
        }
        Map<String,List> map = new HashMap<>();
        map.put("group",groupList);
        map.put("user",userList);
        return map;

    }

    public Map<String,List> findUserByGroupIdFallBack(long id)
    {
        Map<String,List> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add("controller断路器启动，执行 FailBack 方法");
        list.add("参数：{ "+id+" }");
        map.put("断路器",list);
        return map;
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping(value = "/test")
    public Map<String,String> test() {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("UserService");

        Map<String,String> map = new HashMap<>();

        map.put("UserService",serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

        return map;

    }

    @Value("${profile}")
    private String profile;
    @GetMapping(value = "/profile")
    public String getProfile(){
        return profile;
    }

}
